/*
 * Copyright (C) 2004 - 2011 John Currier
 * Copyright (C) 2016 Rafal Kasa
 *
 * This file is a part of the SchemaSpy project (http://schemaspy.org).
 *
 * SchemaSpy is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * SchemaSpy is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package org.schemaspy.output.dot.schemaspy;

import org.schemaspy.output.dot.RuntimeDotConfig;

/**
 * Format table data into .dot format to feed to Graphvis' dot program.
 *
 * @author John Currier
 * @author Rafal Kasa
 * @author Nils Petzaell
 */
public final class DotConfigHeader implements Header {

    private final RuntimeDotConfig runtimeDotConfig;
    private final boolean showLabel;

    public DotConfigHeader(
            final RuntimeDotConfig runtimeDotConfig,
            final boolean showLabel
    ) {
        this.runtimeDotConfig = runtimeDotConfig;
        this.showLabel = showLabel;
    }

    @Override
    public String value() {
        final StringBuilder header = new StringBuilder();
        header.append("  graph [");
        final boolean rankdirbug = runtimeDotConfig.isRankDirBugEnabled();
        if (!rankdirbug) {
            header.append("    rankdir=\"RL\"");
        }
        header.append("    bgcolor=\"" + runtimeDotConfig.styleSheet().getBodyBackground() + "\"");
        if (showLabel) {
            if (rankdirbug) {
                header.append("    label=\"\\nLayout is significantly better without '-rankdirbug' option\"");
            } else {
                header.append("    label=\"\\nGenerated by SchemaSpy\"");
            }
            header.append("    labeljust=\"l\"");
        }
        header.append("    nodesep=\"0.18\"");
        header.append("    ranksep=\"0.46\"");
        header.append("    fontname=\"" + runtimeDotConfig.getFont() + "\"");
        header.append("    fontsize=\"" + runtimeDotConfig.getFontSize() + "\"");
        header.append("    packmode=\"graph\"");
        header.append("  ];");
        header.append("  node [");
        header.append("    fontname=\"" + runtimeDotConfig.getFont() + "\"");
        header.append("    fontsize=\"" + runtimeDotConfig.getFontSize() + "\"");
        header.append("    shape=\"plaintext\"");
        header.append("  ];");
        header.append("  edge [");
        header.append("    arrowsize=\"0.8\"");
        header.append("  ];");
        return header.toString();
    }
}
